package com.cloudOffice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cloudOffice.constant.HttpConstant;
import com.cloudOffice.constant.SystemConstant;
import com.cloudOffice.execption.CustomException;
import com.cloudOffice.execption.SystemException;
import com.cloudOffice.model.system.SysUser;
import com.cloudOffice.result.Result;
import com.cloudOffice.service.SysUserService;
import com.cloudOffice.vo.system.SysUserQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @program: cloud-office
 * @className: LoginController
 * @description: 用户管理
 * @data: 2024/2/7 16:11
 * @author: ihu
 * @version: 1.0
 **/
@Slf4j
@Tag(name = "用户管理")
@RestController("SysUserController")
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
	
	@Resource
	private SysUserService sysUserService;
	
	/**
	 * 用户条件分页查询
	 */
	@GetMapping("{page}/{limit}")
	@Operation(summary = "用户条件分页查询")
	public Result<IPage<SysUser>> index(@PathVariable Long page,
	                                    @PathVariable Long limit,
	                                    SysUserQueryVo sysUserQueryVo) {
		log.info("用户条件分页查询：sysUserQueryVo: {}", sysUserQueryVo);
		return Result.success(sysUserService.index(page, limit, sysUserQueryVo));
	}
	
	/**
	 * 获取用户
	 */
	@GetMapping("get/{id}")
	@Operation(summary = "获取用户")
	public Result<SysUser> get(@PathVariable Long id) {
		log.info("获取用户：id: {}", id);
		return Result.success(sysUserService.getById(id));
	}
	
	/**
	 * 新增用户
	 */
	@PostMapping("save")
	@Operation(summary = "新增用户")
	public Result<?> save(@RequestBody SysUser user) {
		log.info("保存用户：user: {}", user);
		isExistUsername(user);
		sysUserService.save(user);
		return Result.success();
	}
	
	/**
	 * 更新用户
	 */
	@PutMapping("update")
	@Operation(summary = "修改用户")
	public Result<?> updateById(@RequestBody SysUser user) {
		log.info("修改用户：user: {}", user);
		isExistUsername(user);
		sysUserService.updateById(user);
		return Result.success();
	}
	
	/**
	 * 删除用户
	 */
	@DeleteMapping("remove/{id}")
	@Operation(summary = "删除用户")
	public Result<?> remove(@PathVariable Long id) {
		log.info("删除用户：id: {}", id);
		if (sysUserService.getById(id) == null)
			throw new CustomException(HttpConstant.CODE_404, SystemConstant.SYSUSER_NOT_EXISTS);
		sysUserService.removeById(id);
		return Result.success();
	}
	
	/**
	 * 更新状态
	 * 用户状态：1-正常 0-停用
	 */
	@Operation(summary = "更新状态")
	@GetMapping("updateStatus/{id}/{status}")
	public Result<?> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
		log.info("更新状态：id: {}, status: {}", id, status);
		sysUserService.updateStatus(id, status);
		return Result.success();
	}
	
	/**
	 * 判断用户名是否存在
	 */
	private void isExistUsername(SysUser user) {
		SysUser sysUser = sysUserService.getOne(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getUsername,
				user.getUsername()));
		if (user.getId() != null) {
			if (sysUser == null)
				throw new SystemException(HttpConstant.CODE_400, SystemConstant.SYSUSER_NOT_EXISTS);
			if (!sysUser.getId().equals(user.getId()))
				throw new SystemException(HttpConstant.CODE_400, SystemConstant.SYSUSER_USERNAME_EXISTS);
		} else {
			if (sysUser != null)
				throw new SystemException(HttpConstant.CODE_400, SystemConstant.SYSUSER_EXISTS);
		}
	}
}
